/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:PocketBookApp"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"

  You can also use Blend to do all this with the tool's support.
  See http://www.galasoft.ch/mvvm
*/

using GalaSoft.MvvmLight;
using PocketBook.UI.Infrastructure;
using Ninject;
using System;
using System.IO;
using System.Xml.Linq;

namespace PocketBook.UI.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator
    {
        private static MainWindowViewModel _main;
        public static DependencyResolver Kernel { get; set; }
        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            Kernel = new DependencyResolver();
            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time services and viewmodels
            ////}
            ////else
            ////{
            ////    // Create run time services and view models
            ////}
            this.CheckApplicationDirectories();
            _main = new MainWindowViewModel();
        }

        /// <summary>
        /// Gets the Main property which defines the main viewmodel.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainWindowViewModel Main
        {
            get
            {
                return _main;
            }
        }

        private void CheckApplicationDirectories()
        {
            var doc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string path = System.IO.Path.Combine(doc, "Pocket Book Data");


            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);

                XDocument expense = new XDocument(new XElement("expenses"));
                expense.Save(path + "\\expenses.xml");

                XDocument incomes = new XDocument(new XElement("incomes"));
                incomes.Save(path + "\\incomes.xml");
            }
            else
            {
                if (!File.Exists(path + "\\expenses.xml"))
                {
                    XDocument expense = new XDocument(new XElement("expenses"));
                    expense.Save(path + "\\expenses.xml");
                }

                if (!File.Exists(path + "\\incomes.xml"))
                {
                    XDocument incomes = new XDocument(new XElement("incomes"));
                    incomes.Save(path + "\\incomes.xml");
                }
            }
        }
    }
}